<template>
  <div>正在登录...</div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useTokenStore } from '@/stores/token.js'
import { getUserInfoService } from '@/api/user.js'
import { ElMessage } from 'element-plus'

const router = useRouter()
const tokenStore = useTokenStore()

onMounted(async () => {
  try {
    const response = await getUserInfoService()
    if (response.data.token) {
      tokenStore.setToken(response.data)
      ElMessage.success('登录成功！')
      router.push('/main/all')
    } else {
      ElMessage.error(response.data.message || '获取用户信息失败')
      router.push('/login')
    }
  } catch (error) {
    ElMessage.error('请求用户信息失败')
    router.push('/login')
  }
})
</script>